package com.changgou.order.task;

import com.alibaba.fastjson.JSON;
import com.changgou.order.config.RabbitMQConfig;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author kfzhao
 * @version 1.0
 * @className QueryPointTask
 * @description
 * @date 2022/1/4 0:18
 */
@Component
@Slf4j
public class QueryPointTask {
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询任务表中的最新数据
     *
     * @param
     * @return void
     * @throws
     * @description
     * @author kfzhao
     * @date 2022/1/4 0:18
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void queryTask() {
        // 1.获取小于系统当前时间的数据
        List<Task> taskList = taskMapper.findTaskLessThanCurrentTime(new Date());
        if (!CollectionUtils.isEmpty(taskList)) {
            // 2.将任务发送到消息队列
            for (Task task : taskList) {
                rabbitTemplate.convertAndSend(RabbitMQConfig.EX_BUYING_ADDPOINTUSER, RabbitMQConfig.CG_BUYING_ADDPOINT_KEY, JSON.toJSONString(task));
                log.info("订单服务向添加积分队列发送了一条消息");
            }
        }
    }
}
